﻿using System.Security.Cryptography;
using System.Text;

namespace WCS.Entities
{
    public class MD5Utils
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Md5(string password)
        {
            StringBuilder sb = new StringBuilder();
            var md5 = MD5.Create();
            //将输入字符串转换为字节数组并计算哈希。
            byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
            for (int i = 0; i < data.Length; i++)
            {
                //加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
                sb.Append(data[i].ToString("x2"));
            }
            return sb.ToString();
        }
        /// <summary>
        /// MD5验证
        /// </summary>
        /// <param name="password"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        public static bool VerifyMD5(string password, string hash)
        {
            string hashOfInput = Md5(password);
            if (hashOfInput.CompareTo(hash) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

}
